package son.vpoint.dinokids.view;

import org.anddev.andengine.util.MathUtils;

import son.vpoint.dinokids.GameActivity;
import son.vpoint.dinokids.R;
import son.vpoint.dinokids.until.MathFactory;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

public class GameView extends View {
	private GameActivity mActivity;
	private Context context;
	private int x ,y;
	private Integer[] arrGhimX, arrGhimY;
	public MathFactory mMathFactory;
	public Bitmap mBitmapBang;
	public Bitmap mBitmapCong;
	public Bitmap mBitmapTru;
	public Bitmap mBitmapNhan;
	public Bitmap mBitmapChia;
	public Bitmap mBitmapHoi;
	public Bitmap mBitmapOvuong;
	public Bitmap[] mBitmapGhim;
	public Paint mPaint;
	
	public GameView(Context context) {
		super(context);
		mActivity = new GameActivity();
		mMathFactory = new MathFactory();
		mBitmapHoi = BitmapFactory.decodeResource(getResources(),
				R.drawable.dau_hoi);
		mBitmapOvuong = BitmapFactory.decodeResource(getResources(),
				R.drawable.khung);
		mBitmapBang = BitmapFactory.decodeResource(getResources(),
				R.drawable.bang);
		mBitmapCong = BitmapFactory.decodeResource(getResources(),
				R.drawable.cong);
		mBitmapTru = BitmapFactory.decodeResource(getResources(),
				R.drawable.tru);
		mBitmapNhan = BitmapFactory.decodeResource(getResources(),
				R.drawable.nhan);
		mBitmapChia = BitmapFactory.decodeResource(getResources(),
				R.drawable.chia);
		mBitmapGhim = new Bitmap[5];
		mBitmapGhim[0] = BitmapFactory.decodeResource(getResources(), R.drawable.ghim1);
		mBitmapGhim[1] = BitmapFactory.decodeResource(getResources(), R.drawable.ghim2);
		mBitmapGhim[2] = BitmapFactory.decodeResource(getResources(), R.drawable.ghim3);
		mBitmapGhim[3] = BitmapFactory.decodeResource(getResources(), R.drawable.ghim4);
		mBitmapGhim[4] = BitmapFactory.decodeResource(getResources(), R.drawable.ghim5);
		
		
		Typeface mFontNumber = Typeface.createFromAsset(context.getAssets(),
				"fonts/vnibaybuom.ttf");

		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setStrokeWidth(50);
		mPaint.setTextSize(200F);
		mPaint.setTypeface(mFontNumber);

		
		x = mActivity.getWidth()/10;
		y = 3*mActivity.getHeight()/4;
		arrGhimX = new Integer[] { x, 2 * x - x / 6, 3 * x - x / 3,
				4 * x - x / 2, 4 * x+x/2, 5 * x + x / 2,
				6 * x +x/2, 7 * x + x / 2, 8 * x+x/2 };
		arrGhimY = new Integer[]{y-50, y-50, y-50, y-50, y-50, y-50, y-50, y-50, y-50};
		

	}

	@Override
	protected void onDraw(Canvas canvas) {
		
	
		

		for(int i =0 ; i<9 ; i++){
			int k =MathUtils.random(0, 4);
			canvas.drawBitmap(mBitmapGhim[k], arrGhimX[i], arrGhimY[i], mPaint);
		}

		if(mMathFactory.getDifficultLevel() ==0){
			if(this.mMathFactory.getParam1() == -1 ){
				canvas.drawBitmap(mBitmapHoi, 150, 320, mPaint);
				canvas.drawBitmap(mBitmapOvuong, 150, 320, mPaint);
				canvas.drawBitmap(mBitmapCong, 305, 320, mPaint);
				canvas.drawText(""+ mMathFactory.getParam2(), 470, 450, mPaint);
				canvas.drawBitmap(mBitmapBang, 560, 320, mPaint);
				canvas.drawText(""+ mMathFactory.getResult(), 730, 450, mPaint);
				
			}
			if(this.mMathFactory.getParam1() != -1 ){
				
				canvas.drawBitmap(mBitmapHoi, 430, 320, mPaint);
				canvas.drawBitmap(mBitmapOvuong, 420, 320, mPaint);
				canvas.drawBitmap(mBitmapCong, 250, 320, mPaint);
				canvas.drawText(""+ mMathFactory.getParam1(), 150, 450, mPaint);
				canvas.drawBitmap(mBitmapBang, 560, 320, mPaint);
				canvas.drawText(""+ mMathFactory.getResult(), 750, 450, mPaint);
			}
			
		}

		
		invalidate();
		super.onDraw(canvas);
		

	}
}
